package com.kindness.frate.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.kindness.frate.R;
import com.kindness.frate.widget.contextmenu.ContextMenuDialogFragment;
import com.kindness.frate.widget.contextmenu.MenuObject;
import com.kindness.frate.widget.contextmenu.MenuParams;
import com.kindnesslibrary.adapter.CommonAdapter;
import com.kindnesslibrary.adapter.ViewHolder;
import com.kindnesslibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class LeftDeleteActivity extends BaseActivity {

    RecyclerView rv_content;
    TextView tv_title;
    private List<String> datas = new ArrayList<>();
    CommonAdapter commonAdapter;
    private ContextMenuDialogFragment mMenuDialogFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_left_delete_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        rv_content = getView(R.id.rv_content);
        rv_content.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        tv_title = getView(R.id.tv_title);
        fragmentManager = getSupportFragmentManager();
        datas.add("");
        datas.add("");
        datas.add("");
        datas.add("");
        datas.add("");
        datas.add("");
        datas.add("");
        datas.add("");
        commonAdapter = new CommonAdapter<String>(this, R.layout.item_left_delete_layout, datas){
            @Override
            public void convert(ViewHolder holder, String s) {
                TextView tv_delete = holder.getView(R.id.tv_delete);
                tv_delete.setOnClickListener(v -> {
                    commonAdapter.remove(holder.getIndex());
                    commonAdapter.notifyItemRangeChanged(0, commonAdapter.getItemCount());
                    commonAdapter.notifyDataSetChanged();
                });
            }
        };
        rv_content.setAdapter(commonAdapter);
        commonAdapter.notifyDataSetChanged();

        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
//        mMenuDialogFragment.setItemClickListener(this);
//        mMenuDialogFragment.setItemLongClickListener(this);

    }

    private List<MenuObject> getMenuObjects() {
        List<MenuObject> menuObjects = new ArrayList<>();
        MenuObject close = new MenuObject();
        close.setResource(R.mipmap.icn_close);
        MenuObject send = new MenuObject("Send message");
        send.setResource(R.mipmap.icn_1);
        MenuObject like = new MenuObject("Like profile");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.icn_2);
        like.setBitmap(b);
        MenuObject addFr = new MenuObject("Add to friends");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.mipmap.icn_3));
        addFr.setDrawable(bd);
        MenuObject addFav = new MenuObject("Add to favorites");
        addFav.setResource(R.mipmap.icn_4);
        MenuObject block = new MenuObject("Block user");
        block.setResource(R.mipmap.icn_5);
        menuObjects.add(close);
        menuObjects.add(send);
        menuObjects.add(like);
        menuObjects.add(addFr);
        menuObjects.add(addFav);
        menuObjects.add(block);
        return menuObjects;
    }

    @Override
    protected void initEvent() {
        tv_title.setOnClickListener(v -> {
            if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
            }
        });
    }
}
